package abcy.cloud.syscoreserver.services.impls;

import abcy.cloud.blogcommon.enums.SignEnum;
import abcy.cloud.blogcommon.utils.CodeUtil;
import abcy.cloud.blogcommon.utils.UUIDUtil;
import abcy.cloud.syscoreserver.dto.req.SysPermissionAddReqDto;
import abcy.cloud.syscoreserver.dto.req.SysPermissionsSelectReqDto;
import abcy.cloud.syscoreserver.dto.req.SysRolePermissionAddReqDto;
import abcy.cloud.syscoreserver.dto.resp.SysPermissionSelectRespDto;
import abcy.cloud.syscoreserver.dto.resp.SysRoleSelectRespDto;
import abcy.cloud.syscoreserver.entities.SysPermission;
import abcy.cloud.syscoreserver.mappers.SysPermissionMapper;
import abcy.cloud.syscoreserver.mappers.SysRoleMapper;
import abcy.cloud.syscoreserver.mappers.SysRolePermissionMapper;
import abcy.cloud.syscoreserver.services.SysPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.util.*;

@Service
@Slf4j
public class SysPermissionServiceImpl implements SysPermissionService {

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Override
    public PageInfo<SysPermissionSelectRespDto> getPermissionsForPage(SysPermissionsSelectReqDto sysPermissionsSelectReqDto) {
        PageHelper.startPage(sysPermissionsSelectReqDto.getPageIndex(), sysPermissionsSelectReqDto.getPageSize());
        List<SysPermissionSelectRespDto> permissionSelectRespDtoList = sysPermissionMapper.getPermissionsForPage(sysPermissionsSelectReqDto);
        PageInfo<SysPermissionSelectRespDto> pageInfo = new PageInfo<>(permissionSelectRespDtoList);
        return pageInfo;
    }

    @Override
    public void addPermission(SysPermissionAddReqDto sysPermissionAddReqDto) {
        SysPermission sysPermission = new SysPermission();
        BeanUtils.copyProperties(sysPermissionAddReqDto,sysPermission);
        SysPermissionSelectRespDto sysPermissionSelectResp = sysPermissionMapper.getPermissionById(sysPermissionAddReqDto.getParentId());
        String permissionCode = CodeUtil.codeGenerator(SignEnum.PERMISSION.getValue(), sysPermissionMapper.getMaxPermissionsCode());
        sysPermission.setCode(permissionCode);
        if(Objects.nonNull(sysPermissionSelectResp)) {
            List<String> parentIds = Arrays.asList(sysPermissionSelectResp.getParentIds().split(","));
            List<String> pslist = new ArrayList<>();
            for(String val: parentIds) {
                pslist.add(val);
            }
            pslist.add(sysPermissionSelectResp.getParentId());
            String pids = StringUtils.join(pslist);
            sysPermission.setParentIds(pids);
        }
        Date date = new Date();
        sysPermission.setCreateTime(date);
        sysPermission.setUpdateTime(date);
        sysPermission.setId(UUIDUtil.UUID());
        sysPermissionMapper.insert(sysPermission);
    }

    @Override
    public Map<String, Object> selectPermissionByConditions(Map<String, Object> map) {
        SysPermissionsSelectReqDto sysPermissionsSelectReqDto = new SysPermissionsSelectReqDto();
        String userId = map.get("userId").toString();
        sysPermissionsSelectReqDto.setUserId(userId);
        List<SysRoleSelectRespDto> roleSelectRespDtos = sysRoleMapper.selectRolesByUserId(userId);
        List<String> list = new ArrayList<>();
        roleSelectRespDtos.stream().forEach(role -> list.add(role.getId()));
        list.add("1");
        sysPermissionsSelectReqDto.setRoleIds(list);
        List<SysPermissionSelectRespDto> list1 =  sysPermissionMapper.selectPermissionByConditions(sysPermissionsSelectReqDto);
        Map<String, Object> map1 = new HashMap<>();
        List<String> list2 = new ArrayList<>();
        list1.stream().forEach(e -> list2.add(e.getUrl()));
        map1.put("permissions", list2);
        return map1;

    }


}
